package org.videopractice.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;

import org.videopractice.BR;
import org.videopractice.R;
import org.videopractice.common.PermissionsCheckerHelper;
import org.videopractice.common.StringUtils;
import org.videopractice.databinding.ActivityMainBinding;
import org.videopractice.media.RecordScreen;

public class MainActivity extends FragmentActivity {

    private MainViewModel mainViewModel = new MainViewModel();

    private final int RECORD_REQUEST_CODE = 1;
    private RecordScreen recordScreen;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding viewDataBinder = DataBindingUtil.setContentView(this, R.layout.activity_main);
        viewDataBinder.setVariable(BR.mainViewModel, mainViewModel);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == RECORD_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                recordScreen.getMediaProjection(resultCode, data);

                DisplayMetrics dm = getResources().getDisplayMetrics();
                int width = dm.widthPixels;
                int height = dm.heightPixels;
                recordScreen.startCapture(width, height);
            } else {
                Toast.makeText(this, "requestRecordFail: 用戶拒绝录制屏幕", Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }


    private void doAfterAuthorizedHandle(boolean isSucceed) {
        if(recordScreen == null) {
            recordScreen = new RecordScreen(this);
        }
        if(recordScreen.checkReady()) {
            // 开始录屏
            DisplayMetrics dm = getResources().getDisplayMetrics();
            int width = dm.widthPixels;
            int height = dm.heightPixels;
            recordScreen.startCapture(width, height);
        } else {
            Intent captureIntent = recordScreen.requestRecording();
            if(captureIntent != null) {
                startActivityForResult(captureIntent, RECORD_REQUEST_CODE);
            }
        }
    }

    private void requestRecordScreenAction() {
        String[] permissions = new String[] {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.MODIFY_AUDIO_SETTINGS,
                Manifest.permission.RECORD_AUDIO//音频
        };

        PermissionsCheckerHelper checkerHelper = new PermissionsCheckerHelper(this, this::doAfterAuthorizedHandle);
        if(checkerHelper.checkPermission(permissions)){
            doAfterAuthorizedHandle(true);
        }
    }

    private void stopRecordScreenAction() {
        if(recordScreen != null) {
            recordScreen.stopCapture();
        }
    }

    public void recordScreenOnClick(View view) {
        if(mainViewModel.isStartRecord) {
            stopRecordScreenAction();
        } else {
            requestRecordScreenAction();
        }
        mainViewModel.toggle();
    }

    public void showScreenOnClick(View view) {
        // 显示录屏
        if(StringUtils.isEmpty(mainViewModel.ipAddress)) {
            Toast.makeText(this, "请填写IP", Toast.LENGTH_LONG).show();
            return;
        }

        Intent intent = new Intent();
        intent.putExtra("ipAddress", mainViewModel.ipAddress);
        intent.setClass(this, ShowScreenActivity.class);
        startActivity(intent);
    }



    public class MainViewModel extends BaseObservable {

        private boolean isStartRecord = false;

        private String ipAddress;

        public void toggle() {
            isStartRecord = !isStartRecord;
            notifyPropertyChanged(BR.recordScreenText);
        }

        public void setIpAddress(String ipAddress) {
            this.ipAddress = ipAddress;
        }

        @Bindable
        public CharSequence getRecordScreenText() {
            return isStartRecord ? "停止录屏" : "开始录屏";
        }

        @Bindable
        public String getIpAddress() {
            return ipAddress;
        }
    }

}
